﻿/*
 * 由SharpDevelop创建。
 * 用户： 86168
 * 日期: 2020/11/22
 * 时间: 16:09
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace 超级计算器
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		float firstnum = 0;
        char op = '=';
        bool newNum = true;
        float ratio = 1f;
        string[] typeselect={"面积","长度","质量","功率","密度","能量","体积","加速度","压力","压强","速度"};
        string[] unitselect=System.IO.File.ReadAllLines("unit.txt");
        string[] ratioselect=System.IO.File.ReadAllLines("ratio.txt");
		public MainForm(){
			InitializeComponent();
			const String sign = "789/456*123-0.=+";
            var buttonsize = new Size(panel.Width / 4, panel.Height / 4);
            var button = new Button[16];
            for(int i = 0; i < 16; i++){
                button[i] = new Button(){
                    Name = "button" + i.ToString(),
                    Text = sign[i].ToString(),
                    Parent = panel,
                    Location = new Point((i%4)*buttonsize.Width,(i/4)*buttonsize.Height),
                    Size = buttonsize
                };
                button[i].Click += "+-*/=".IndexOf(sign[i]) < 0 ? new EventHandler(Number_Click) : new EventHandler(Operater_Click);
            }
            typeSelect.Items.AddRange(typeselect);
		}
		private void Number_Click(object sender,EventArgs e){
            Screen.Text =newNum?((Button)sender).Text: Screen.Text + ((Button)sender).Text;
            newNum = false;
        }
        private void Operater_Click(object sender, EventArgs e){
        	newNum = true;
            float value = 0;
            try{
                value = float.Parse(Screen.Text);
            }
            catch(Exception ex){
                Screen.Text = ex.Message;
                return;
            };
            Func<float, float, float>[] opor = { (m, v) => m + v, (m, v) => m - v, (m, v) => m * v, (m, v) => m / v, (m, v) => v };
            firstnum = opor["+-*/=".IndexOf(op)](firstnum, value);
            op = ((Button)sender).Text[0];
            Screen.Text = firstnum.ToString();
        }
        void typeSelectSelectedIndexChanged(object sender,EventArgs e){
        	int i = ((ComboBox)sender).SelectedIndex;
        	string[] Items = unitselect[i].Split(',');
        	typeFrom.Items.Clear();
        	typeFrom.Items.AddRange(Items);
        	typeTo.Items.Clear();
        	typeTo.Items.AddRange(Items);
        	typeFrom.SelectedIndex = typeTo.SelectedIndex = 0;
        }
        void unitSelectedIndexChange(object sender,EventArgs e){
        	try{
        		string[] shit = ratioselect[typeSelect.SelectedIndex].Split(';');
        		ratio = float.Parse(shit[typeTo.SelectedIndex])/float.Parse(shit[typeFrom.SelectedIndex]);
        		textChange(sender,e);
        	}
        	catch{}
        }
        void textChange(object sender,EventArgs e){
        	try{
        		unitTo.Text=(float.Parse(unitFrom.Text)*ratio).ToString();
        	}
        	catch(Exception ex){
        		unitTo.Text=ex.Message;
        	}
        }
	}
}
